<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>班级信息</title>
	<%
    	String path = request.getContextPath();
	    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
    %>
	<c:set var="path" value="<%=basePath %>"></c:set>
 	<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
 	<link href="css/public.css" rel="stylesheet" />
 	
 	<!--皮肤文件-->
	<link rel="stylesheet" type="text/css" href="./css/easyui.css" />
	<!--图标文件-->
	<link rel="stylesheet" type="text/css" href="./css/icon.css" />
	<link rel="stylesheet" type="text/css" href="./css/demo.css" />
	<!--jquery文件-->
	<script type="text/javascript" src="./js/jquery.min.js"></script>
	<!--easyui核心文件-->
	<script type="text/javascript" src="./js/jquery.easyui.min.js"></script>
	<!--对应的语言包-->
	<script type="text/javascript" src="./js/easyui-lang-zh_CN.js"></script>
</head>
<body> 
	<div id="tb">
		<a id="btn" href="goClassDit" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加班级</a>
		<a id="btn" href="goClasshb" class="easyui-linkbutton" data-options="iconCls:'icon-add'">合班</a>
	</div>
	<table id="dg1"></table> 
	<script type="text/javascript">
		//数据表格
		$(function(){
			dolog();
		});
		function dolog(){
			 	$('#dg1').datagrid({loadFilter:pagerFilter}).datagrid({    
			 	url:'classList',
			    iconCls: 'icon-edit',  //加载图标
				singleSelect: true,    //单行选中
				rownumbers:true,
				pagination:true,//开启分页功能
				pageSize:5,    
				pageNumber:1,
				pageList: [5,10,20],
				border:false,
				sortName:'clazz_id',
				sortOrder:'desc',
				fitColumns:true,
				striped:true,
				toolbar: '#tb',
				fit:true,
			    columns:[[    
			    	{
			        	field:'clazz_id',
			        	title:'班级编号',
			        	width:80,
			        	align:'center'
			        },{
			        	field:'clazz_name',
			        	title:'班级名称',
			        	width:80,
			        	align:'center'
			        },{
			        	field:'clazz_createtime',
			        	title:'创建时间',
			        	width:80,
			        	align:'center',
			        	formatter:function(value, row, index){
			        		var time = new Date(row.clazz_createtime);
				   			var birthday= time.getFullYear()+"年"+(time.getMonth()+1)+"月"+time.getDate()+"日";
			        		return birthday;
			        	}
			        },{
			        	field:'users',
			        	title:'创建人',
			        	width:80,
			        	align:'center',
			        	formatter:function(value, row, index){
			        		if(row.users==null){
			        			return "该用户已注销";
			        		}else{
			        			return row.users.user_username;s
			        		}
			        	}
			        },{
			        	field:'specialty',
			        	title:'专业',
			        	width:80,
			        	align:'center',
			        	formatter:function(value, row, index){
			        		if(row.specialty==null){
			        			
			        			return "该专业已注销";
			        		}else{
			        			return row.specialty.specialty_name;
			        		}
			        		
			        	}
			        },{
			        	field:'count',
			        	title:'人数',
			        	width:80,
			        	align:'center'
			        },{   
			        	field:'operate',
			        	title:'操作',
			        	align:'center',
			        	width:$(this).width()*0.2,
			        	formatter:function(value, row, index){
			        		var str = '<a href="#" onclick="del('+row.clazz_id+')" name="opera" class="easyui-linkbutton"></a>';
			        		str += '<a href="classesave?classId='+row.clazz_id+'" name="opera1" class="easyui-linkbutton"></a>';
			        		return str;
			        	}
			        }
			    ]],
			    onLoadSuccess:function(data){
			            $("a[name='opera']").linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
			            $("a[name='opera1']").linkbutton({text:'修改',plain:true,iconCls:'icon-save'});
			    },
			   
			}); 
		}
		function getcount(id){
			$.ajax({
				url:'getStuByClass',
				datatype:'json',
				data:{
					id:id
				},
				success:function(data){
					return data.size;
				} 
			});
		}
		function del(id){
			if(confirm("确实要删除吗？")){
				$.ajax({
					url:'getStuByClass',
					datatype:'json',
					data:{
						id:id
					},
					success:function(data){
						console.log(data.size);
						if(data.size>0){
							 alert('请先将该班级的学生先更换班级再进行删除');
						}else{
							deleteClass(id);
							dolog();
						}
					} 
				});
            }else {
                alert("已经取消了删除操作");
            }
		}
		function deleteClass(id){
			$.ajax({
				url:'delClass',
				datatype:'json',
				data:{
					id:id
				},
				success:function(data){
					alert("删除成功");
				} 
			});
		}
		//前台分页功能
		function pagerFilter(data){
		    if (typeof data.length == 'number' && typeof data.splice == 'function'){    // 判断数据是否是数组
		        data = {
		            total: data.length,
		            rows: data
		        }
		    }
		    var dg = $(this);
		    var opts = dg.datagrid('options');
		    var pager = dg.datagrid('getPager');
		    pager.pagination({
		        onSelectPage:function(pageNum, pageSize){
		            opts.pageNumber = pageNum;
		            opts.pageSize = pageSize;
		            pager.pagination('refresh',{
		                pageNumber:pageNum,
		                pageSize:pageSize
		            });
		            dg.datagrid('loadData',data);
		        }
		    });
		    if (!data.originalRows){
		        data.originalRows = (data.rows);
		    }
		    var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
		    var end = start + parseInt(opts.pageSize);
		    data.rows = (data.originalRows.slice(start, end));
		    return data;
		}
	</script>
</body>
</html>